package com.demo.accessibility.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import androidx.appcompat.app.AppCompatActivity
import com.demo.accessibility.service.NurseHelperService
import com.demo.accessibility.utils.AccessibilityUtils
import com.android.nursehelper.utils.AutoFiringUtils
import com.demo.accessibility.utils.FloatWindowPermissionUtils
import com.demo.accessibility.utils.PermissionUtils
import com.blankj.utilcode.util.ToastUtils
import com.demo.accessibility.interfaces.OnPermissionResult
import com.hjq.bar.OnTitleBarListener
import com.hjq.bar.TitleBar
import com.demo.accessibility.R
import kotlinx.android.synthetic.main.activity_permission_guide.*

/**
 * ================================================
 * 权限管理界面
 * Created by ligang on 2023/9/16
 * ================================================
 */
class PermissionGuideActivity : AppCompatActivity() {
    companion object {
        fun start(context: Context) {
            context.startActivity(Intent(context, PermissionGuideActivity::class.java))
        }

        fun checkAllMustPermission(context: Context): Boolean {
            return FloatWindowPermissionUtils.checkPermission(context)
                    && AccessibilityUtils.isAccessibilitySettingsOn(
                context,
                NurseHelperService::class.java
            )
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_permission_guide)
        tb_permission_guide_title.setOnTitleBarListener(object : OnTitleBarListener {
            override fun onLeftClick(titleBar: TitleBar?) {
                onBackPressed()
            }

            override fun onRightClick(titleBar: TitleBar?) {

            }
        })
        bt_float.setOnClickListener {
            FloatWindowPermissionUtils.requestPermission(this, object : OnPermissionResult {
                override fun permissionResult(isOpen: Boolean) {
                }
            })
        }
        bt_acc.setOnClickListener {
            startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS))
        }
        tv_setting2.setOnClickListener {
            AutoFiringUtils.start(this)
        }
        tv_setting1.setOnClickListener {
            PermissionUtils.ignoredBatteryOptimization(this) { result ->
                if (result == "hasIgnored") {
                    ToastUtils.showLong("你已忽略电池优化")
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        checkPermission()
    }


    private fun checkPermission() {
        var notAllowCount = 2
        //检测是否开启了悬浮窗口权限
        if (FloatWindowPermissionUtils.checkPermission(this)) {
            bt_float.isEnabled = false
            bt_float.text = getString(R.string.permissions_guide_open_already)
            tv_float.isSelected = true
            notAllowCount--
        } else {
            bt_float.isEnabled = true
            bt_float.text = getString(R.string.permissions_guide_open_go)
            tv_float.isSelected = false
        }

        //检测是否开启无障碍权限
        if (AccessibilityUtils.isAccessibilitySettingsOn(
                this,
                NurseHelperService::class.java
            )
        ) {
            bt_acc.isSelected = true
            bt_acc.text = getString(R.string.permissions_guide_open_already)
            tv_acc.isSelected = true
            notAllowCount--
        } else {
            bt_acc.isSelected = false
            bt_acc.text = getString(R.string.permissions_guide_open_go)
            tv_acc.isSelected = false
        }

        if (notAllowCount > 0) {
            tv_hint.text =
                String.format(getString(R.string.permissions_guide_not_allow), notAllowCount)
            tv_hint.setTextColor(getColor(R.color.app_them_orange_color))
            tv_count.text = "$notAllowCount"
            tv_count.setBackgroundResource(R.drawable.ic_permission_count_bg)
        } else {
            tv_hint.text = getString(R.string.permissions_guide_tips3)
            tv_hint.setTextColor(getColor(R.color.app_them_color))
            tv_count.text = ""
            tv_count.setBackgroundResource(R.drawable.ic_permission_ok_bg)
        }
    }
}